﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using Einstein.Group.BLL;
using Einstein.Data;

namespace Einstein.Platform.Window.Common
{
    public partial class Common_Dictionary_New : OfficeForm
    {
        public Common_Dictionary_New()
        {
            InitializeComponent();
        }

        public bool IsEdit
        {
            get;
            set;
        }
        public Node ModelID { get; set; }
        public void BindModel()
        {
            if (IsEdit)
            {
                txtParent.Text = ModelID.Parent.Text;
                if (ModelID.Tag.ToString() == "Sub")
                {
                    txtKeyName.Text = ModelID.Text;
                    txtKeyValue.Visible = false;
                    labelX2.Visible = false;
                    requiredFieldValidator4.Enabled = false;
                }
                if (ModelID.Tag.ToString() == "Child")
                {
                    txtKeyName.Text = ModelID.Text;
                    txtKeyValue.Value = Convert.ToInt32(ModelID.Cells[1].Text);
                }
            }
            else//新增
            {
                if (ModelID.Tag.ToString() == "Main")
                {
                    txtParent.Text = ModelID.Text;
                    txtKeyValue.Visible = false;
                    labelX2.Visible = false;
                    requiredFieldValidator4.Enabled = false;
                }

                if (ModelID.Tag.ToString() == "Sub")
                {
                    txtParent.Text = ModelID.Text;
                }
            }

        }

        private void Add_Edit()
        {
            if (superValidator1.Validate())
            {
                Dictionary info = new Dictionary();
                info.ID = Convert.ToInt32(ModelID.DataKey);
                info.Category = txtParent.Text.Trim();
                info.KeyName = txtKeyName.Text.Trim();
                info.KeyValue = txtKeyValue.Value;
                bool result = false;

                if (DictionaryBLL.ExistKeyName(info, IsEdit))
                {
                    MyMessageBox.ShowMessage(this, "已经存在相同的项目，请重试。");
                    txtKeyName.Focus();
                    return;
                }

                if (IsEdit)
                {
                    if (ModelID.Tag.ToString() == "Child")
                    {
                        result = DictionaryBLL.Update(info);
                    }
                    else
                    {
                        result = DictionaryBLL.UpdateCategory(info.KeyName,ModelID.Text);
                    }
                }
                else
                {
                    result = DictionaryBLL.Add(info);
                }
                if (result)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MyMessageBox.ShowMessage(this, "保存失败，请重试。");
                }
            }
        }



        private void btn_Click(object sender, EventArgs e)
        {
            ButtonX btn = sender as ButtonX;
            if (btn != null)
            {
                switch (btn.Name)
                {
                    case "btn_OK":
                        Add_Edit();
                        break;
                    case "btn_Cancel":
                        {
                            this.DialogResult = DialogResult.No;
                            this.Close();
                        }
                        break;
                }
            }
        }

    }
}
